20 results
1 - Getting Started
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp 1-40
-
- Chapter
- Export citation
-
Summary
In the software industry across the globe, there is a need to develop large and complex software. This software should be platform independent, internet enabled, easy to modify, secure, and robust. To meet these requirements object-oriented paradigm has been advocated. Based on this paradigm, Java programming language emerges as the best programming environment. Java is used for mobile programming, internet programming, and many other applications compatible with distributed systems. This book aims to cover the essential components of Java programming so that the readers can improve their skills, cope with the demands of the IT industry and solve the problems in their own field of study.
Introduction
Necessity IT industry operates in such a diverse environment that software programs should not be confined to a single PC. Rather, they should move from a single user environment to mainframes, to networks, to network of networks, and so on. A giant program can be developed which integrates numerous mini programs which were developed by geographically distributed programmers, and all of them can be connected online. Such a distributed programming techniques and environments are no longer a dream but a reality, and it is Java, the Internet programming language, which makes it possible.
Scope Java was developed by Sun Microsystems Inc. with the intent to create a dynamic, object-oriented programming (OOP) language, suitable for using the same types of development tasks as C and C++, but without the difficulties and bugs common to those languages. This is why Java has been touted as “a better C++”; it contains all the essential OOP features, but with lower complexities than C++. Sun describes Java as a “simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, and dynamic language.”
Your learning In this chapter we shall briefly highlight the various features of Java programming, its superiority over other programming languages, and the tools available for its programming. At the end, you will be able to run your first program in Java.
Concept of Programming
Application systems Computer programming is used to build a software system (e.g., Microsoft Word, Library Information System, Internet Explorer browser, mobile app, printer’s driver software, to name a few). These software systems take an input and then produce an output (Figure 1.1).
List of Tables
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp xix-xxii
-
- Chapter
- Export citation
2 - Java Language Basics
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp 41-145
-
- Chapter
- Export citation
-
Summary
Any language is characterized by two features: alphabets and grammar. To learn a language, it is necessary to learn these two features first. Once you have learned a language, you will be able to express a thought or idea, such as a poem, in the language. Language is used for communication and Java programming language is not an exception. Java language is used to express a programmer’s instructions to solve a problem just like other programming languages, for example, C, C++, Python, etc. Java looks a lot similar to C++, and, by extension, like C. If you are an experienced C or C++ programmer, this chapter is mostly redundant for you. Java enables a programmer to write robust programs. This chapter aims to teach you all the basic features of Java that a programmer should know.
Introduction
Language elements At this point, we are assuming that you have successfully installed Java and were able to run the sample programs given in Chapter 1. It is time now to start programming. As a Java programmer, it is necessary to have full understanding of the elements of the language like keywords, literals, data types, operators, loops, branches, etc.
Strongly typed language Java programming language mostly follows syntax similar to that of C/ C++ to write statements and expressions. So if you are familiar with C/ C++, it will be easy for you to understand Java. As with all modern programming languages like C/ C++, Java is a strongly typed language, which means all data to be used should be defined their types in programs. Java supports several types of data, which are called primitive data types. In this chapter we will examine several data types, their structures, operations on them, and how they can be utilized to solve problems. Constants and variables are the two basic programming elements. It is necessary to learn how the variables of different data types can be defined. Java has a very good composition of defining arrays of elements of different types, which is also an interesting concept to learn. You may use different data types to declare variables and to create arrays of elements.
Frontmatter
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp i-iv
-
- Chapter
- Export citation
5 - Package and Interface
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp 287-359
-
- Chapter
- Export citation
-
Summary
Package and interface are two incredible concepts in Java programming language. These features make Java programming suitable for developing large software in any application domain. The concept of package and interface allows the programmers to organize classes and hence methods and fields or constants for developing a large software program. This concept is called programming in large. It also helps the programmers to write code in their own styles, which is called programming in small. With this you will feel Java is like C when programming in small and like Smalltalk (a pure object-oriented programming language) when programming in large. This means that Java is powerful like any function-oriented programming language while you are coding individual lines, and has the extensibility and expressive power of a pure object-oriented programming language while you’re designing.
Introduction
From design to coding The separation of “designing” from “coding” was one of the most fundamental advances in programming style in the recent past, and object-oriented languages are capable of implementing a strong form of this separation. According to this programming paradigm, you design the classes first and decide the relationships between these classes, and then you implement the Java code needed for each of the methods in your class design. The advantages with this style is that, you can change your mind about aspects of the design without affecting anything but small, local pieces of your programs; vice-versa, you can change the implementation of any method without affecting the design.
To achieve this, the Java developer includes two very innovative features: package and interface. Packages provide class repositories, grouping them together, and controlling their access to the outside world. Interfaces provide a way of grouping abstract method definitions and sharing them among classes.
Your learning This chapter plans to discuss the package and interface concepts in Java. You will learn how to design, use, and create your own packages and interfaces. The specific topics which you will learn include: a detailed discussion of designing classes and coding classes; what are packages and why they are useful for designing classes; using other available packages in your own classes; how you can create your own packages; what interfaces you can utilize in terms of code reuse and design; designing and working with interfaces; and many more.
List of Programs
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp xxiii-xxxii
-
- Chapter
- Export citation
Contents
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp vii-xiv
-
- Chapter
- Export citation
Dedication
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp v-vi
-
- Chapter
- Export citation
7 - Multithreading
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp 425-518
-
- Chapter
- Export citation
-
Summary
Java is the most suitably programming language for developing application software for a distributed environment with concurrent execution. To enable this, Java has been designed as a multithreaded programming language. This means that a program can be executed with one or more threads to solve a problem while utilizing the computing resources in the most efficient manner. Since the inception of Java, an elegant feature called “multithreading” has been introduced. With this unique feature, a programmer can write a program with multiple flows of executions along with robust control in the executions. More significantly, Java is a lightweight programming language that is designed to have a very small memory footprint, it has minimalist syntax and features, and it is easy to port an implementation to different systems.
Introduction
Why multithreading? Multitasking is a very common features in today’s computation environment. Java supports thread-based approach to multi-tasking, which is more precisely called multithreading. Multithreading means multiple flow of control. Multithreading programming is a conceptual paradigm for programming where one can divide a program into two or more processes which can be run in parallel. There are two main advantages of multithreading: First, a program with multiple threads will, in general, result in better utilization of system resources, including the CPU, because another line of execution can use the CPU when one line of execution is idle or blocked. Second, there are several problems which are solved better by multiple threads. For example, we can easily write a multithreaded program to show animation, play music, display documents, and download files from the network, etc., all of them at the same time.
Heavyweight versus lightweight programming Multitasking involves complex processing that an execution environment has to perform while it executes the program. If processes are heavyweight tasks, they require their own separate address spaces. The inter-process communication becomes expensive and reduces the advantages of the concurrent execution. Context switching from one process to another is also costly. Java follows the concept of threads and thus the concept of thread-based multitasking. All the threads are lightweights, which share the same address space. Hence interthread communication remains inexpensive and context switching from one thread to the next is at a lower cost.
List of Symbols
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp xxxiii-xxxiv
-
- Chapter
- Export citation
4 - Inheritance
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp 207-286
-
- Chapter
- Export citation
-
Summary
Java developer aims at Java programming suitable for fast, large, complex, and safety-critical software development. One important requirement towards this is to code reusability. Code reusability helps a programmer to use code which is already developed by someone else. For this reason, Java developer introduces one of the most magnificent object-oriented programming features called inheritance. This feature allows a programmer to access fields and methods of an existing class to their new class under development. Inheritance thus facilitates inheriting some of the characteristics in a child class from its parent class. It is just like a ladder from the child class to the parent class. In addition to code sharing, access privilege is also taken into consideration in the inheritance mechanism. This enables protection of data and code from unauthorized access.
Introduction
Why inheritance? Class is the basic element of any software in object-oriented system design approach. In the last chapter, you have learned about how to write classes in Java programs. In software development, a software is under constant updation, for example, extension (from one version to the next or from one release to another, etc.), or modification (correction of some faults or to make it compatible with new technology, hardware, etc.). The requirement is that all the development should take place without disturbing the existing system and, of course, with minimum effort and time.
Your learning Inheritance in Java is a clever mechanism to support these requirements. It allows an efficient and secure way to share code, modify it, and reuse it. With the help of inheritance, you will learn how to extend a given class and thus access its fields and methods. You will also learn how to hide some fields and methods from a parent class to the extended class.
Concept of Inheritance
Basic concept The inheritance concept is related to taxonomy or classification. For example, Figure 4.1 shows taxonomy (in partial form) of animals. It looks like a tree (this is why it is also called and inheritance tree). This tree bears the information that lion and hyena, for example, have some common characteristics. In addition, they have their own characteristics.
8 - Java IO and File
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp 519-653
-
- Chapter
- Export citation
-
Summary
Input and output are two most important operations in any software system design. This is particularly significant with modern Internet and communication technology where there are many modes of input–output from many sources. This versatility of input–output brings a challenge to software systems developer. Fortunately, Java developer has java.io, which provides a brilliant method of managing data in an easy, programmer-friendly, reliable, and robust manner. Today java.io API is admired as one of the finest packages which facilitates data manipulation in the best possible way. The addendum java. nio offers several features to implement intensive and high-speed IO operations. This chapter gives you an understanding of handling input-output in programs in Java.
Introduction
Real-world programming requirements In the previous chapters, you have studied a little about input and output related methods in Java. Chapter 2 mentioned input to programs and output from programs considering the programs are simple and text-based (reading from keyboard), and writing output on the console (display). However, in many real world applications, input–output are more versatile: inputs are from many different sources (such as files, other programs, network channels, etc.); similarly, outputs from a program may go into several destinations like files, external programs, network lines in web, etc. (Figure 8.1).
Java’s key to success While a majority of programming languages are keyboard–console input and output (IO), Java is much ahead of them, supporting efficient, strong, and flexible real-world programming with networking, files, and many more options. Java includes the java.io package which contains nearly every class you might ever need to perform IO. As a unique concept, Java introduced stream for flow of different forms of data to-and-fro the sources and destinations. There are numerous classes representing different streams for input sources and output destinations. The streams in the java.io package support many kinds of data such as primitives, object, localized characters, etc.
The java.io package is vast. It defines over seventy classes and interfaces, many of which have a large number of methods. Covering all of them in detail is beyond the scope of this book and often it is not necessary for a programmer to know about them.
List of Figures
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp xv-xviii
-
- Chapter
- Export citation
Preface
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp xxxv-xxxviii
-
- Chapter
- Export citation
-
Summary
Welcome to the wonderful world of Java programming and thank you for your interest in this book. We would like to share some thoughts behind writing this book.
Background
With the growth of Information and Communication Technology (ICT), there is a need to develop large and complex software. To meet this requirement, object-oriented programming paradigms are popular among software professionals. The software is required to be platform independent, Internet enabled, easy to modify and update, secure, and robust. To cater to all these requirements, Java programming language appears to be the most suitable object-oriented programming language as it meets all these requirements. Java was developed by Sun Microsystems in 1995 and has emerged as the best programming environment according to the Time magazine. Today, Java is used for mobile programming, Internet programming, software development, and for numerous other applications in distributed systems and commercial hardware products. This book aims to cover the essential features of Java programming so that software professionals can improve their skills to cope with the current demands of the IT industry. In addition, this book can help students and faculty members to learn the programming language with the minimum effort and solve many problems in their fields.
Why This Book?
The need: Java is vast. Unlike other programming languages, Java is constantly evolving. The latest version, Java 14 or JDK 14, was released on March 17, 2020. With every new release, many features are deprecated and many new features are added. Developers keep on adding new features so that the programming language can meet the changing needs of the software industry. Thus Java has become vast, with a large number of features. However, this is at the cost of increasing the difficulty in learning the language. It is not easy for programmers to grasp the numerous syntaxes and semantics that the language contains in its present form. As experienced teachers of Computer Science and Engineering, we know how to modulate a discussion so that learners can grasp the concepts with minimum cognitive load. The learners need to feel they have the competence to learn and start loving the language. In our opinion, existing books fail to do so. Hence the need of a new book like this.
Joy with Java
- Fundamentals of Object Oriented Programming
- Debasis Samanta, Monalisa Sarma
-
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023
-
- Textbook
- Export citation
-
The Java programming language has been one of the most powerful tools available to computer programmers since its inception in 1995. It has also consistently changed since then, making it a vast and powerful resource for object-oriented programming today. This lucid textbook introduces the student not only to the nuances of object-oriented programming, but also to the many syntaxes and semantics of the modern Java language. Each concept of programming is explained, and then illustrated with small but effective ready-to-run programs. Important points to be noted have been emphasized and hints have been given at the end of each discussion so that programmers are careful to avoid common pitfalls. Finally, a number of practice problems taken from real world scenarios encourage the student to think in terms of problem solving, consolidating the knowledge gained.
6 - Exception Handling
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp 360-424
-
- Chapter
- Export citation
-
Summary
There is a need for software developers to build reliable and robust software. To make such a software, the 80:20 rule has to be followed, that is, 80 percent of the effort should go into checking and handling errors, and only 20 percent in writing the software. Error detection and error handling remain an important issue in software development. Java arms developers with an elegant mechanism for handling errors that produces efficient and organized error-handling code. This mechanism is a unique feature in Java and called exception handling. Exception handling allows developers to detect errors easily without writing special code to test return values. Even better, it lets the programmer keep exception-handling code cleanly separated from the exception-generating code. It also lets the programmer use the same exception-handling code to deal with a range of possible exceptions.
This chapter devotes to cover the fascinating concept of exception handling in Java.
Introduction
What is the issue? Programmers in any language endeavor to write bug-free programs, programs that never crash, programs that can handle any situation efficiently, and that can recover from unusual situations without causing the users any undue inconvenience. Good intentions aside, programs that cover all these points don’t exist. In real life, errors occur, either because the programmer didn’t anticipate every situation the code would get into (or did not have the time to test the programs enough), or because of situations outside the programmer’s control like bad data from users, corrupt files that do not have the right data in them, network connections that do not connect, hardware devices that do not respond, to name a few.
Requirement In Java, unusual events that may cause a program to fail are called exceptions. The dictionary meaning of “exception” is “an abnormal situation.” What will happen to the program if an abnormal situation occurs? For example, what result will your calculations return when divide x by y when y = 0? Or, if you want to store a value in the array in its 100th location while the size of the array is 50? In practice, in such a situation, your execution will be suspended immediately, for which you are not really prepared.
Index
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp 755-766
-
- Chapter
- Export citation
Acknowledgments
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp xxxix-xl
-
- Chapter
- Export citation
3 - Encapsulation
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp 146-206
-
- Chapter
- Export citation
-
Summary
Java is an object-oriented programming language. This means that a program is a collection of objects. Note that in the real world, everything is defined as an object and an object can interact with other objects. Java programming follows this philosophy. This means that writing a Java program means you need to create your objects and also mention how your objects interact with other objects which, thus, solves the problem. How an object can be created and the mechanism of object–object interaction, etc., are realized by means of one of the most important object-oriented programming paradigms called encapsulation. This chapter includes discussions related to this programming feature. Indeed, the concept of encapsulation is very important for any programmer to build their skill in Java programming.
Introduction
Object and its concept Objects in this real world are physical entities, which can be seen and touched. For example, trees, tables, computers, fans, people, birds, and so on. Each object consists of several parts. For example, a tree consists of root, trunk, leaves, buds, flowers, fruits, seeds, and so on. Note that a part of the tree can again be another object, for example, a fruit. An object can be defined by its parts. Such a definition would make the object unique and distinguishable from other objects. In addition to the physical elements, objects are also characterized by behavioral parts. For example, a person is an object and has some behavior, such as climbing (a tree), plucking (a flower), cooking (food), and so on. Objects can be used to model our real world (which is also an object!) and then different activities in the world can be carried out.
Object-oriented approach This is the philosophy of how we can build a system using the object-oriented concept. A system can be built on a bottom-up approach. Create the smallest objects. Use several small objects to build larger objects; larger objects are then assembled to build much larger objects, and so on, and then finally the target system is built. For example, if you want to make a computer, then build transistors, use transistors to build logic gates, use logic gates to build circuits, use circuits to build different functional units, use different functional units to build memory, CPU, Keyboard, display unit, and finally, all this makes up your computer.
9 - Generic Programming
- Debasis Samanta, Indian Institute of Technology, Kharagpur, Monalisa Sarma, Indian Institute of Technology, Kharagpur
-
- Book:
- Joy with Java
- Published online:
- 08 November 2023
- Print publication:
- 15 June 2023, pp 654-754
-
- Chapter
- Export citation
-
Summary
The task of a programmer is to write a program which implements an algorithm. An algorithm specifies operations on data. Data can be of different types and structures. Thus, a program is an amalgamation of algorithm and data structures. Implementing the same algorithm for each different data structure, that is, just code duplication, is cumbersome. The concept of generic programming addresses this issue. With generic programming, a program can be written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. In this way, programs are related to abstract data types and such programming helps the programmer to avoid code duplication. Java features generic programming facilities and this chapter will help you to master the subject.
Introduction
Concept of generic The dictionary meaning of the word “generic” is “not specific.” This word is used as an adjective to imply a characteristic of relating to a class or group of things. For example, a generic drug, which refers to the chemical makeup of a drug and not specific to a brand name under which the drug is sold.
Definition of generic programming In the world of programming, generic is used to mean an abstraction. The term “generic programming” was coined by David R. Musser and Alexander A. Stepanov in 1989 and defined as follows:
“Generic programming centers around the idea of abstracting from concrete, efficient algorithms to obtain generic algorithms that can be combined with different data representations to produce a wide variety of useful software.”
Polymorphism versus generic programming At an early stage of generic programming (like in LISP, Ada, Smalltalk, etc.) the concept was to write common functions that differ only in the set of types on which they operate when used, thus reducing duplication. Such software entities are known as generics. Generic programming can thus be termed broadly as parametric polymorphism. The polymorphism concept is an important object-oriented programming paradigm which you are already familiar with in the context of encapsulation and inheritance. Method overloading is parametric polymorphism.
Why generic programming? With method overloading you can define two or more methods having the same name but with different signatures and different operations. So codes need to be developed for each method.